package com.newy.web.misc;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newy.core.BaseController;
import com.newy.core.util.Converter;
import com.newy.core.util.LangUtils;
import com.newy.domain.misc.FqaPost;
import com.newy.domain.misc.FqaType;
import com.newy.service.misc.FqaService;

/**
 * 
 * @author linzongxue 2012-2-19
 *
 */
@Controller
@RequestMapping("misc/fqa")
public class FqaController extends BaseController {
	@Resource private FqaService fqaSvc;

	/*===== FQA类型 =====*/
	
	@RequestMapping("getType") @ResponseBody
	public FqaType getType(Long id){
		return fqaSvc.getType(id);
	}
	
	@RequestMapping("saveType") @ResponseBody
	public FqaType saveType(FqaType type){
		fqaSvc.saveType(type);
		return type;
	}
	
	@RequestMapping("deleteType") @ResponseBody
	public void deleteType(Long id){
		fqaSvc.deleteType(id);
	}
	
	/**
	 * 获取类型树数据
	 * @param node extjs传入过来的父类型
	 */
	@RequestMapping("getTypeChildren") @ResponseBody
	public List<Map<String, Object>> getTypeChildren(String node){
		List<FqaType> types = fqaSvc.getTypeChildren(LangUtils.parseLong(node, 0L));
		return new Converter().changeField("text", "name").toListOfMap(types);
	}

	/*===== FQA内容 =====*/
	
	@RequestMapping("getPost") @ResponseBody
	public FqaPost getPost(Long id){
		return fqaSvc.getPost(id);
	}
	
	@RequestMapping("savePost") @ResponseBody
	public FqaPost savePost(FqaPost post){
		fqaSvc.savePost(post);
		return post;
	}
	
	@RequestMapping("deletePosts") @ResponseBody
	public void deletePosts(Long[] ids){
		for (Long id : ids){
			fqaSvc.deletePost(id);
		}
	}

	@RequestMapping("getAllPostsOfType") @ResponseBody
	public List<Map<String, Object>> getAllPostsOfType(Long typeId){
		List<FqaPost> posts = fqaSvc.getAllPostsOfType(typeId);
		return new Converter()
			.addField("typeName", "typeId", FqaType.class, "name")
			.toListOfMap(posts);
	}
}
